/**
 * Copyright 2013-2033 Xia Jun(3979434@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p>
 * **************************************************************************************
 * *
 * Website : http://www.farsunset.com                           *
 * *
 * **************************************************************************************
 */
package com.farsunset.cim.controller;

import com.farsunset.cim.sdk.server.session.CIMSession;
import com.farsunset.cim.sdk.server.session.DefaultSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ViewController {

    @Autowired
    private DefaultSessionManager defaultSessionManager;

    @RequestMapping("/welcome")
    @ResponseBody
    public Object welcome(HttpServletRequest request) throws Exception {
        Map<String,Object> resultMap= new HashMap<>();
        resultMap.put("osName", System.getProperty("os.name"));
        resultMap.put("osVersion", System.getProperty("os.version"));
        resultMap.put("javaVersion", System.getProperty("java.version"));
        resultMap.put("inetAddress", InetAddress.getLocalHost().getHostAddress());
        resultMap.put("loopbackAddress", InetAddress.getLoopbackAddress());
        resultMap.put("serverInfo", request.getSession().getServletContext().getServerInfo());
        resultMap.put("count", defaultSessionManager.queryAll().size());
        return resultMap;
    }


    @RequestMapping("/userData")
    @ResponseBody
    public Object userData() {
        Map<String, Object> result = new HashMap<>();
        List<CIMSession> CIMSessions = defaultSessionManager.queryAll();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", CIMSessions.size());
        result.put("data", CIMSessions);

        return result;
    }

}